package com.sky.service.impl;

import com.sky.entity.OrderDetail;
import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.ReportService;
import com.sky.vo.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.*;

@Service
public class ReportServiceImpl implements ReportService {

    private final OrderMapper orderMapper;

    public ReportServiceImpl(OrderMapper orderMapper) {
        this.orderMapper = orderMapper;
    }

    @Autowired
    private UserMapper userMapper;

    /**
     * 营业额统计
     * @param begin
     * @param end
     * @return com.sky.vo.TurnoverReportVO
     * @author zhupp
     * @create 2025/2/28
     **/
    @Override
    public TurnoverReportVO turnoverStatistics(LocalDate begin, LocalDate end) {

        // 遍历出每个日期。以字符串的形式拼接："2001-1-1,2001-1-2"
        Map<LocalDate, Double> lastMap = new LinkedHashMap<>();
        while (!begin.isAfter(end)){
            begin = begin.plusDays(1);
            lastMap.put(begin,0.0);
        }

        // 获取每一天的营业额，返回一个Map<LocalDate,Double>
        List<TurnoverVo> turnoverVoList = orderMapper.getByDate();

        turnoverVoList.forEach(tvl->{
            lastMap.forEach((key,value)->{
                if (key.isEqual(tvl.getDate())){
                    lastMap.put(tvl.getDate(),tvl.getTotalAmount());
                }
            });
        });

        List<LocalDate> dateList = new ArrayList<>();
        List<Double> amountList = new ArrayList<>();
        lastMap.forEach((key,value)->
        {
                dateList.add(key);
                amountList.add(value);
        });
        // 构造TurnoverReportVO对象
        return TurnoverReportVO.builder()
                .dateList(StringUtils.join(dateList,","))
                .turnoverList(StringUtils.join(amountList,","))
                .build();
    }

    /**
     * 用户统计
     * @param begin
     * @param end
     * @return com.sky.vo.UserReportVO
     * @author zhupp
     * @create 2025/2/28
     **/
    @Override
    public UserReportVO userStatistics(LocalDate begin, LocalDate end) {
        // 遍历出每个日期。以字符串的形式拼接："2001-1-1,2001-1-2"
        Map<LocalDate, Integer> lastMap = new LinkedHashMap<>();
        while (!begin.isAfter(end)){
            begin = begin.plusDays(1);
            lastMap.put(begin,0);
        }

        List<LocalDate> dateList = new ArrayList<>();
        List<Integer> newCountList = new ArrayList<>();
        List<Integer> totalCountList = new ArrayList<>();

        // 获取每一天的新建用户的数量
        List<UserVO> newUserCount = userMapper.getNewUserCount();

        // 将对应日期的用户和数量插入到lastMap中
        newUserCount.forEach(u->{
            lastMap.forEach((key,value)->{
                if (key.isEqual(u.getDate())){
                    lastMap.put(u.getDate(),u.getCount());
                }
            });
        });
        // 将对应的时间和新用户数量插入到list中
        lastMap.forEach((k,v)->{
            dateList.add(k);
            newCountList.add(v);
        });

        // 获取每一天总的用户数量
        for (LocalDate date : dateList){
            Integer totalUserCount = userMapper.getTotalUserCount(date.plusDays(1));
            totalCountList.add(totalUserCount);
        }

        // 将对应时间的总用户数量插入到list中

        return UserReportVO.builder()
                .dateList(StringUtils.join(dateList,','))
                .newUserList(StringUtils.join(newCountList,','))
                .totalUserList(StringUtils.join(totalCountList,','))
                .build();
    }

    /**
     * 订单统计
     * @param begin
     * @param end
     * @return com.sky.vo.OrderReportVO
     * @author zhupp
     * @create 2025/2/28
     **/
    @Override
    public OrderReportVO ordersStatistics(LocalDate begin, LocalDate end) {

        // 遍历出每个日期。以字符串的形式拼接："2001-1-1,2001-1-2"
        Map<LocalDate, Integer> lastMap = new LinkedHashMap<>();

        Map<LocalDate, Integer> lastMap2 = new LinkedHashMap<>();
        while (!begin.isAfter(end)){
            begin = begin.plusDays(1);
            lastMap.put(begin,0);
            lastMap2.put(begin,0);
        }

        // 有效订单数、
        List<Integer> validOrder = List.of(3, 4, 5);
        Integer validOrderCount = orderMapper.selectCountByStatus(validOrder,null);
        // 取消订单数、
        List<Integer> invalidOrder = List.of(6, 7);
        Integer invalidOrderCount = orderMapper.selectCountByStatus(invalidOrder,null);
        // 已完成订单数、
        List<Integer> completedOrder = List.of(5);
        Integer completedOrderCount = orderMapper.selectCountByStatus(completedOrder,null);
        // 总订单数
        Integer totalOrderCount = orderMapper.selectCountByStatus(null,null);

        // 订单完成率
        double orderCompletionRate =(double)completedOrderCount /validOrderCount;

        // 日期列表 、订单数列表
        List<LocalDate> dateList = new ArrayList<>();
        List<Integer> orderCountList = new ArrayList<>();

        // 获取对应日期的和状态数量的对象
        List<OrderVO> orderCount = orderMapper.getorderCount(end.plusDays(1),null);

        //将对应的日期插入
        orderCount.forEach(count ->{
            lastMap.forEach((key,value)->{
                if (key.isEqual(count.getDate())){
                    lastMap.put(count.getDate(), count.getCount());
                }
            });
        });
        lastMap.forEach((k,v)->{
            dateList.add(k);
            orderCountList.add(v);
        });


        // 有效订单数列表
        List<Integer> validOrderCountList = new ArrayList<>();
        List<OrderVO> OrderVO = orderMapper.getorderCount(end.plusDays(1),validOrder);
        //将对应的日期插入
        OrderVO.forEach(list ->{
            lastMap2.forEach((key,value)->{
                if (key.isEqual(list.getDate())){
                    lastMap2.put(list.getDate(), list.getCount());
                }
            });
        });
        // 将对应的订单表数量插入到list中
        lastMap2.forEach((k,v)->{
            validOrderCountList.add(v);
        });

        return OrderReportVO.builder()
                .dateList(StringUtils.join(dateList,","))
                .orderCompletionRate(orderCompletionRate)
                .orderCountList(StringUtils.join(orderCountList,","))
                .totalOrderCount(totalOrderCount)
                .validOrderCount(validOrderCount)
                .validOrderCountList(StringUtils.join(validOrderCountList,","))
                .build();
    }

    /**
     * Top10
     * @param begin
     * @param end
     * @return com.sky.vo.SalesTop10ReportVO
     * @author zhupp
     * @create 2025/2/28
     **/
    @Override
    public SalesTop10ReportVO salesTop10ReportVO(LocalDate begin, LocalDate end) {
        // 获取排名的top10的商品的名字和销量
        List<OrderDetail> OrderDetailList = orderMapper.getNameTop10(begin,end);

        List<String> nameList = new ArrayList<>();
        List<Integer> numberList = new ArrayList<>();
        OrderDetailList.forEach(orderDetail->{
            numberList.add(orderDetail.getNumber());
            nameList.add(orderDetail.getName());
        });
        return SalesTop10ReportVO.builder()
                .nameList(StringUtils.join(nameList,','))
                .numberList(StringUtils.join(numberList,','))
                .build();
    }
}
